var ojurl = "http://localhost/OnlineJudge";
var tokenName = "token";

// function login() {
//     $.ajax({
//         type: "post",
//         url: "http://aliyun.seatang.cn:8080/onlinejudge/test/getInfoById",
//         beforeSend: function(request) {
//             request.setRequestHeader("token", token);
//         },
//         timeout: 10000,
//         contentType: "application/json",
//         data: JSON.stringify(jsonDate),
//         dataType: "json",
//         success: function(data){
//             console.log('ok');
//         },
//         error:function(){
//             console.log('error');
//         }
//     });
// }
function appendToken(request) {
    if (localStorage.getItem(tokenName) != null) {
        request.setRequestHeader(tokenName, localStorage.getItem(tokenName).toString());
    }
}
function storeInfoAfterLogin() {
    localStorage.setItem("name", resultData.name);
    localStorage.setItem("token", resultData.token);
}
function clearInfo() {
    localStorage.removeItem("name");
    localStorage.removeItem("token");
}

function getVerifyCode(obj) {
    $.ajax ({
        type: "GET",
        url: ojurl + "/common/verifyCode",
        dataType: "json",
        timeout: 10000,
        success: function(result) {
            document.getElementById(obj).src = "data:image/jpeg;base64," + result.img;
            sessionStorage.setItem("uuid", result.uuid);
        }
    });
}
function getObj(id) {
    return document.getElementById(id);
}
function login(obj, data) {
    $.ajax ({
        type: "POST",
        url: ojurl + "/user/login",
        dataType: "json",
        contentType: "application/json",
        timeout: 10000,
        data: JSON.stringify(data),
        success: function(result) {
            if (result.code == HttpStatus.SUCCESS) {
                resultData = result.data;
                storeInfoAfterLogin();
                var url = "index";
                if (sessionStorage.getItem("preURL") != null) {
                    url = sessionStorage.getItem("preURL").toString();
                }
                window.location = url;
            }
            else {
                var arg = new Object();
                arg.errno = result.code;
                arg.warnObjId = obj;
                arg.message = result.message;
                errorHandle(arg);
            }
        }
    });
}
function register(obj, data) {
    $.ajax ({
        type: "POST",
        url: ojurl + "/user/register",
        dataType: "json",
        contentType: "application/json",
        timeout: 10000,
        data: JSON.stringify(data),
        success: function(result) {
            if (result.code == HttpStatus.SUCCESS) {
                document.getElementById(obj).textContent = "Register OK, Welcome and enjoy our OJ!";
                var list = document.getElementsByClassName("form-group");
                for (var i=0; i<list.length; ++i) {
                    list[i].innerHTML = "";
                }
            }
            else {
                var arg = new Object();
                arg.errno = result.code;
                arg.warnObjId = obj;
                arg.message = result.message;
                errorHandle(arg);
            }
        }
    })
}
function checkToken() {
    $.ajax ({
        type: "GET",
        url: ojurl + "/common/checkToken",
        dataType: "json",
        contentType: "application/json",
        responseType: "application/json",
        timeout: 10000,
        crossDomain: true,
        beforeSend: function (request) {
            appendToken(request)
        },
        success: function(result) {
            var arg = new Object();
            arg.errno = result.code;
            errorHandle(arg);
        }
    })
}
function clearToken() {
    $.ajax ({
        type: "GET",
        url: ojurl + "/common/checkToken",
        dataType: "json",
        contentType: "application/json",
        responseType: "application/json",
        timeout: 10000,
        beforeSend: function (request) {
            appendToken(request)
        },
        success: function(result) {
            if (result.code != HttpStatus.SUCCESS) {
                clearInfo();
                alert("token is expired!");
                window.location.reload;
            }
        }
    })
}
function rankList(page) {
    var jsonData = new Object();
    jsonData.page = String(page);
    jsonData.size = String(rankListPageSize);
    var data = new Object();
    data.data = jsonData;
    $.ajax({
        type: "POST",
        url: ojurl + "/user/rankList",
        dataType: "json",
        contentType: "application/json",
        timeout: 10000,
        data: JSON.stringify(data),
        success: function(result) {
            if (result.code == HttpStatus.SUCCESS) {
                showRankList(result.data);
            }
            else {
                alert(result.message);
            }
        }
    })
}

function status(jsonData) {
    var subUrl = "/solution/statusSearch";
    if (jsonData.problemId == null && jsonData.name == null && jsonData.lang == null && jsonData.state == null) {
        subUrl = "/solution/status";
    }
    jsonData.size = String(statusPageSize);
    var data = new Object();
    data.data = jsonData;
    $.ajax({
        type: "POST",
        url: ojurl + subUrl,
        dataType: "json",
        contentType: "application/json",
        timeout: 10000,
        data: JSON.stringify(data),
        success: function(result) {
            if (result.code == HttpStatus.SUCCESS) {
                showStatus(result.data);
            }
            else {
                alert(result.message);
            }
        }
    })
}

function showCode(solutionId) {
    var jsonData = new Object();
    jsonData.solutionId = String(solutionId);
    jsonData.isContest = "0";
    var data = new Object();
    data.data = jsonData;
    $.ajax({
        type: "POST",
        url: ojurl + "/code/downloadCode",
        dataType: "json",
        contentType: "application/json",
        beforeSend: function (request) {
            appendToken(request)
        },
        timeout: 10000,
        data: JSON.stringify(data),
        success: function(result) {
            if (result.code == HttpStatus.SUCCESS) {
                var code = result.data.code;
                document.getElementById("myModalLabel").innerHTML = "solutionId: " + String(solutionId);
                document.getElementById("showCode").value = code;
                $("#myModal").modal("show");
            }
            else if (result.code == HttpStatus.UNAUTHORIZED) {
                localStorage.removeItem("token");
                localStorage.removeItem("name");
                alert("user login token expired!");
                window.location.reload();
            }
            else {
                alert(result.message);
            }
        }
    })
}

function contestList(jsonData) {
    var subUrl = "/contest/contestSearch";
    if (jsonData.search == null) {
        subUrl = "/contest/contestList";
    }
    jsonData.size = String(contestListPageSize);
    var data = new Object();
    data.data = jsonData;
    $.ajax({
        type: "POST",
        url: ojurl + subUrl,
        dataType: "json",
        contentType: "application/json",
        timeout: 10000,
        data: JSON.stringify(data),
        success: function(result) {
            if (result.code == HttpStatus.SUCCESS) {
                showContestList(result.data);
            }
            else {
                alert(result.message);
            }
        }
    })
}

function problemList(jsonData) {
    var subUrl = "/problem/problemSearch";
    if (jsonData.search == null) {
        subUrl = "/problem/problemList";
    }
    jsonData.size = String(probleListPageSize);
    var data = new Object();
    data.data = jsonData;
    $.ajax({
        type: "POST",
        url: ojurl + subUrl,
        dataType: "json",
        contentType: "application/json",
        beforeSend: function (request) {
            appendToken(request)
        },
        timeout: 10000,
        data: JSON.stringify(data),
        success: function(result) {
            if (result.code == HttpStatus.SUCCESS) {
                showProblemList(result.data);
            }
            else {
                alert(result.message);
            }
        }
    })
}

function submitCode(jsonData) {
    var data = new Object();
    data.data = jsonData;
    $.ajax({
        type: "POST",
        url: ojurl + "/code/submitCode",
        dataType: "json",
        contentType: "application/json",
        beforeSend: function (request) {
            appendToken(request)
        },
        timeout: 10000,
        data: JSON.stringify(data),
        success: function(result) {
            if (result.code == HttpStatus.SUCCESS) {
                window.location.href = "status";
            }
            else if (result.code == HttpStatus.UNAUTHORIZED) {
                localStorage.removeItem("token");
                localStorage.removeItem("name");
                alert("user login token expired!");
                window.location.reload();
            }
            else {
                alert(result.message);
            }
        }
    })
}

function problemInfo(problemId) {
    var jsonData = new Object();
    jsonData.problemId = String(problemId);
    var data = new Object();
    data.data = jsonData;
    $.ajax({
        type: "POST",
        url: ojurl + "/problem/problemInfo",
        dataType: "json",
        contentType: "application/json",
        timeout: 10000,
        data: JSON.stringify(data),
        success: function(result) {
            // console.log(result);
            if (result.code == HttpStatus.SUCCESS) {
                showProblemInfo(result.data);
            }
            else {
                alert(result.message);
            }
        }
    })
}